using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using MyPhotoIndex.Interfaces;
using System.Drawing.Drawing2D;
using MyPhotoIndex.Interfaces.Imaging;

namespace MyPhotoIndex.Controls
{
    public partial class MiniThumbnailViewer : UserControl
    {
        private MyPhotoIndexThumbnailViewer m_parent;
        private bool m_selecting;

        public MiniThumbnailViewer(MyPhotoIndexThumbnailViewer parent)
        {
            m_parent = parent;
            InitializeComponent();

            m_parent.SelectionChangedEvent += new EventHandler<SelectedImageInfo>(m_parent_SelectionChangedEvent);
            this.myPhotoIndexThumbnailViewer1.SelectionChangedEvent += new EventHandler<SelectedImageInfo>(myPhotoIndexThumbnailViewer1_SelectionChangedEvent);
       
            this.tableLayoutPanel1.Paint += new PaintEventHandler(tableLayoutPanel1_Paint);
            this.myPhotoIndexThumbnailViewer1.BackColor = Color.FromArgb(197, 220, 248);
        }

        void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
        {
            Color color = Color.FromArgb(197, 220, 248);
            using (Brush brush = new SolidBrush(color))
            {
                e.Graphics.FillRectangle(brush, e.ClipRectangle);
            }
            
            e.Graphics.FillEllipse(Brushes.Blue, new Rectangle(5, 7, 20, 20));
            e.Graphics.FillEllipse(Brushes.Blue, new Rectangle(this.Width - 25, 7, 20, 20));
        }

        void myPhotoIndexThumbnailViewer1_SelectionChangedEvent(object sender, SelectedImageInfo e)
        {
            if (m_selecting == false)
            {
                m_selecting = true;
                m_parent.ClearSelection();
                m_parent.SelectImage(this.myPhotoIndexThumbnailViewer1.SelectedImages[0], true);
                m_parent.ScrollToSelected();
                m_selecting = false;
            }
        }

        void m_parent_SelectionChangedEvent(object sender, SelectedImageInfo e)
        {
                if (this.Visible == false)
                {
                    return;
                }

                if (m_selecting == false)
                {
                    m_selecting = true;
                    UpdateSelected();
                    m_selecting = false;
                }
        }

        private void UpdateSelected()
        {
            if (m_parent.SelectedImages.Count >= 1)
            {
                //    this.Refresh();
                int selected = m_parent.SelectedImages[m_parent.SelectedImages.Count - 1];
                this.myPhotoIndexThumbnailViewer1.ClearSelection();
                this.SelectImage(selected);
                this.myPhotoIndexThumbnailViewer1.ScrollToSelected();
            }
        }

        protected override void OnVisibleChanged(EventArgs e)
        {
            base.OnVisibleChanged(e);
            if (this.Visible == true)
            {
                UpdateSelected();
            }
        }

        public override void Refresh()
        {
            this.myPhotoIndexThumbnailViewer1.Refresh();
            base.Refresh();
        }

        public IImageCollection ImageList
        {
            set { this.myPhotoIndexThumbnailViewer1.ImageList = value; }
        }

        public void SelectImage(int imageId)
        {
            this.myPhotoIndexThumbnailViewer1.SelectImage(imageId, true);
        }
    }
}
